﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ChoViet.Model;
using ChoViet.Lib.Common;
using System.Text;
using ChoViet.Lib.Mail;
using ChoViet.Site.Properties;
using ChoViet.Site.Models;

namespace ADR.MAG.Controllers
{
    public class AjaxDataController : Controller
    {
        /*
        //
        // GET: /AjaxData/

        ChoVietDataContext eDataContext = new ChoVietDataContext();
        public ActionResult GetDistrict(int id)
        {
            try
            {
                var dic = eDataContext.Districts.Where(d => d.CityID == id).ToList();
                return View(dic);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }
        public ActionResult GetProductCategoryByType(int id)
        {
            try
            {
                List<ProductCategory> proCat = eDataContext.ProductCategories
                                                .Where(c => c.TypeID.HasValue && c.TypeID.Value == id &&
                                                            c.ActiveYN.HasValue && c.ActiveYN.Value == true)
                                                .ToList();
                return View(proCat);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }
        public ActionResult GetPrice(int id)
        {
            try
            {
                List<ConvertPrice> p = eDataContext.ConvertPrices
                                                .Where(c => c.TypeID == id)
                                                .ToList();
                return View(p);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }
        public ActionResult GetArea(int id)
        {
            try
            {
                List<ConvertArea> p = eDataContext.ConvertAreas
                                                .Where(c => c.TypeID == id)
                                                .ToList();
                return View(p);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }
        public ActionResult GetProjectByDistrict(int id)
        {
            try
            {
                List<Project> proj = eDataContext.Projects
                                                .Where(p => p.DistrictID == id && p.PubYN == true)
                                                .ToList();

                return View(proj);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// Author: Takamaru Nguyen.
        /// Date: 03/08/2011.
        /// Description: Update ndownload when users click download document witdh id.
        /// </summary>
        public void UpdateDownload(int id)
        {
            try
            {
                Document doc = eDataContext.Documents.Where(d => d.ID == id).FirstOrDefault();
                doc.nDownload = doc.nDownload + 1;

                eDataContext.SubmitChanges();

                return;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
            }
        }

        public ActionResult GetDemandCategoryByType(int id)
        {
            try
            {
                List<DemandCategory> proCat = eDataContext.DemandCategories
                                                .Where(c => c.DemandTypeID.HasValue && c.DemandTypeID.Value == id &&
                                                            c.ActiveYN.HasValue && c.ActiveYN.Value == true)
                                                .ToList();
                return View(proCat);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }
        public ActionResult GetDemandPrice(int id)
        {
            try
            {
                List<DemandConvertPrice> p = eDataContext.DemandConvertPrices
                                                .Where(c => c.TypeID == id)
                                                .ToList();
                return View(p);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }
        public ActionResult GetDemandArea(int id)
        {
            try
            {
                List<DemandConvertArea> p = eDataContext.DemandConvertAreas
                                                .Where(c => c.TypeID == id)
                                                .ToList();
                return View(p);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }
        public JsonResult SearchTag()
        {
            try
            {
                eDataContext.DeferredLoadingEnabled = false;
                var term = Request["keyword"].ToString();
                var keywords = eDataContext.Tags
                    .Where(e => e.Keyword.IndexOf(term) > -1)
                    .Take(10)
                    .ToList();
                return Json(keywords, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }
        public CaptchaImageResult ShowCaptchaImage()
        {
            return new CaptchaImageResult();
        }
        [HttpPost]
        public ActionResult LoadDistrictByCity(int id)
        {
            try
            {
                var dic = eDataContext.Districts.Where(d => d.CityID == id).ToList();
                return View("ucLoadDistrictByCity", dic);
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Write(ex.Message);
                throw ex;
            }
        }
        public JsonResult BannerClick(int id)
        {
            try
            {
                eDataContext.DeferredLoadingEnabled = false;
                var b = eDataContext.Banners.Where(e => e.ID == id).SingleOrDefault();
                b.nClick += 1;
                eDataContext.SubmitChanges();
                return Json("", JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }
        [ValidateInput(false)]
        public JsonResult AddCommentForArticle(int id)
        {
            try
            {
                eDataContext.DeferredLoadingEnabled = false;
                Comment c = new Comment();
                c.AddBy = Request["addby"] != null ? Request["addby"].ToString() : string.Empty;
                c.AddByEmail = Request["addbyemail"] != null ? Request["addbyemail"].ToString() : string.Empty;
                c.Title = Request["title"] != null ? Request["title"].ToString() : string.Empty;
                c.CommentContent = Request["commentcontent"] != null ? Request["commentcontent"].ToString() : string.Empty;
                c.CommentDate = DateTime.Now;
                c.PublishYN = false;
                eDataContext.Comments.InsertOnSubmit(c);
                eDataContext.SubmitChanges();
                var map = new CommentsInArticle();
                map.CommentID = c.ID;
                map.ArticleID = id;
                eDataContext.CommentsInArticles.InsertOnSubmit(map);
                eDataContext.SubmitChanges();
                return Json(c, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }
        [ValidateInput(false)]
        public JsonResult AddCommentForProduct(int id)
        {
            try
            {
                eDataContext.DeferredLoadingEnabled = false;
                Comment c = new Comment();
                c.AddBy = Request["addby"] != null ? Request["addby"].ToString() : string.Empty;
                c.AddByEmail = Request["addbyemail"] != null ? Request["addbyemail"].ToString() : string.Empty;
                c.Title = Request["title"] != null ? Request["title"].ToString() : string.Empty;
                c.CommentContent = Request["commentcontent"] != null ? Request["commentcontent"].ToString() : string.Empty;
                c.CommentDate = DateTime.Now;
                c.PublishYN = false;
                eDataContext.Comments.InsertOnSubmit(c);
                eDataContext.SubmitChanges();
                var map = new CommentsInProduct();
                map.CommentID = c.ID;
                map.ProductID = id;
                eDataContext.CommentsInProducts.InsertOnSubmit(map);
                eDataContext.SubmitChanges();
                return Json(c, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }
        [ValidateInput(false)]
        public JsonResult AddCommentForProject(int id)
        {
            try
            {
                eDataContext.DeferredLoadingEnabled = false;
                Comment c = new Comment();
                c.AddBy = Request["addby"] != null ? Request["addby"].ToString() : string.Empty;
                c.AddByEmail = Request["addbyemail"] != null ? Request["addbyemail"].ToString() : string.Empty;
                c.Title = Request["title"] != null ? Request["title"].ToString() : string.Empty;
                c.CommentContent = Request["commentcontent"] != null ? Request["commentcontent"].ToString() : string.Empty;
                c.CommentDate = DateTime.Now;
                c.PublishYN = false;
                eDataContext.Comments.InsertOnSubmit(c);
                eDataContext.SubmitChanges();
                var map = new CommentsInProject();
                map.CommentID = c.ID;
                map.ProjectID = id;
                eDataContext.CommentsInProjects.InsertOnSubmit(map);
                eDataContext.SubmitChanges();
                return Json(c, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }
        [ValidateInput(false)]
        public JsonResult AddCommentForVideo(int id)
        {
            try
            {
                eDataContext.DeferredLoadingEnabled = false;
                Comment c = new Comment();
                c.AddBy = Request["addby"] != null ? Request["addby"].ToString() : string.Empty;
                c.AddByEmail = Request["addbyemail"] != null ? Request["addbyemail"].ToString() : string.Empty;
                c.Title = Request["title"] != null ? Request["title"].ToString() : string.Empty;
                c.CommentContent = Request["commentcontent"] != null ? Request["commentcontent"].ToString() : string.Empty;
                c.CommentDate = DateTime.Now;
                c.PublishYN = false;
                eDataContext.Comments.InsertOnSubmit(c);
                eDataContext.SubmitChanges();
                var map = new CommentsInVideo();
                map.CommentID = c.ID;
                map.VideoID = id;
                eDataContext.CommentsInVideos.InsertOnSubmit(map);
                eDataContext.SubmitChanges();
                return Json(c, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }
        [ValidateInput(false)]
        public JsonResult AddCommentForDimand(int id)
        {
            try
            {
                eDataContext.DeferredLoadingEnabled = false;
                Comment c = new Comment();
                c.AddBy = Request["addby"] != null ? Request["addby"].ToString() : string.Empty;
                c.AddByEmail = Request["addbyemail"] != null ? Request["addbyemail"].ToString() : string.Empty;
                c.Title = Request["title"] != null ? Request["title"].ToString() : string.Empty;
                c.CommentContent = Request["commentcontent"] != null ? Request["commentcontent"].ToString() : string.Empty;
                c.CommentDate = DateTime.Now;
                c.PublishYN = false;
                eDataContext.Comments.InsertOnSubmit(c);
                eDataContext.SubmitChanges();
                var map = new CommentInDemand();
                map.CommentID = c.ID;
                map.DemandID = id;
                eDataContext.CommentInDemands.InsertOnSubmit(map);
                eDataContext.SubmitChanges();
                return Json(c, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }
        
        public JsonResult ValidateEmail()
        {
            try
            {
                var email = Request["Email"];
                return Json(!UserManager.CheckByEmail(email), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(false, JsonRequestBehavior.AllowGet);

            }
        }
        public JsonResult ValidateUserName()
        {
            try
            {
                var userName = Request["UserName"];
                return Json(!UserManager.CheckUserName(userName), JsonRequestBehavior.AllowGet);                
            }
            catch
            {
                return Json(false, JsonRequestBehavior.AllowGet);

            }
        }
        */
    }
}
